﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebUI.Helper;
namespace WebUI.Con
{

    public class SaleController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 销售网页中的销售商品下拉级联菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult SaleGoods()
        {
            var sa = OperateContext.Current.BLLSession.IGoodsSortBLL.GetListBy(s => s.GoodsSortId > 0)
                .Select(s => new Model.ViewModel.SaleGoods
                {
                    GoodsSortID = s.GoodsSortId,
                    GoodsSortName = s.GoodsSortName,
                    Goods =
                        s.Goods.Select(t => new Model.ViewModel.saleGood { GoodsID = t.GoodsID, GoodsName = t.GoodsName, MeasureUnite = t.MeasureUnite, Quntity = t.Quntity ?? 0, SalePrice = t.SalePrice ?? 0 })
                }).ToList();
            return PartialView(sa);
        }
        /// <summary>
        /// 插入销售
        /// </summary>
        /// <returns></returns>
        public ActionResult btn_SaleGoods()
        {


            if (Request["SaleNumber"] != null && Request["SaleNumber"].ToString() != "")
            {
                if (Request["SaleObject"] == null || Request["SaleObject"].ToString() == "")
                {
                    Model.SaleTD inser_sale = new Model.SaleTD();
                    inser_sale.SaleGoodsID = Convert.ToInt32(Request["SaleGoodsID"]);
                    inser_sale.IsExchange = Convert.ToBoolean(Request["IsExchange"]);
                    inser_sale.SaleNumber = Convert.ToInt32(Request["SaleNumber"]);
                    inser_sale.SaleUserID = OperateContext.Current.Usr.UserID;
                    inser_sale.SaleID = OperateContext.Current.Shift.ShiftID;
                    inser_sale.SaleTime = DateTime.Now;
                    inser_sale.SaleObject = "普通";
                    int p = OperateContext.Current.BLLSession.ISaleTDBLL.Add(inser_sale);
                    if (p == 1)
                    {
                        Model.Good g_M = new Model.Good();
                        g_M.GoodsID = Convert.ToInt32(Request["SaleGoodsID"]);
                        g_M.Quntity = OperateContext.Current.BLLSession.IGoodBLL.GetListBy_NoTrack(s => s.GoodsID == inser_sale.SaleGoodsID).Select(s => s.Quntity).FirstOrDefault() - Convert.ToInt32(Request["SaleNumber"]);

                        OperateContext.Current.BLLSession.IGoodBLL.Modify(g_M, new string[] { "Quntity" });

                    }
                }
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }


        }
        /// <summary>
        /// 获取销售清单 public DateTime SaleTime { get; set; }

        /// </summary>
        /// <returns></returns>
        public ActionResult Sale_List()
        {
            DateTime PreCheckoutTime =OperateContext.Current.BLLSession.ICheckoutBLL.GetListBy(s => s.CheckoutID > 0, s => s.CheckoutID, false).Select(s=>s.CheckoutTime??DateTime.Now).ToList().FirstOrDefault();

            var sale_list = OperateContext.Current.BLLSession.ISaleTDBLL.GetPagedList(Convert.ToInt32(Request["pageIndex"]) + 1, Convert.ToInt32(Request["pageSize"]), (s => (s.SaleTime ?? DateTime.Now).CompareTo(PreCheckoutTime) > 0), s => s.SaleTime).Select(s => new Model.ViewModel.SaleList()
                {
                    SaleID = s.SaleID,
                    SaleGoodsName = s.Good.GoodsName,
                    SaleUserName = s.UserDB.NickName,
                    SaleNumber = s.SaleNumber ?? 0,
                    SaleObject = s.SaleObject,
                    SaleTime = (s.SaleTime ?? DateTime.Now).ToString("yy年MM月dd日hh时mm分"),
                    SalePrice = (s.Good.SalePrice ?? 0).ToString("0.00"),
                    IsExchange = (s.IsExchange ?? false) ? "兑奖" : "正常",
                    SumSale = ((s.Good.SalePrice ?? 0) * (s.SaleNumber ?? 0)).ToString("0.00")

                }).ToList();
            return Json(sale_list, JsonRequestBehavior.AllowGet);



        }
        [Common.Attributes.Skip]
        /// <summary>
        /// 获取销售清单总数，好分页，同时计算这个销售总价，返回一个msg json对象，含有总页数和总价
        /// </summary>
        /// <returns></returns>

        public ActionResult Sale_List_TotalPage()
        {


            DateTime PreCheckoutTime = OperateContext.Current.BLLSession.ICheckoutBLL.GetListBy(s => s.CheckoutID > 0, s => s.CheckoutID, false).Select(s => s.CheckoutTime ?? DateTime.Now).ToList().FirstOrDefault();

            int Totalpage = OperateContext.Current.BLLSession.ISaleTDBLL.GetListBy(s => (s.SaleTime ?? DateTime.Now).CompareTo(PreCheckoutTime) > 0).Count;
            decimal TotalSale = OperateContext.Current.BLLSession.ISaleTDBLL.GetListBy(s => (s.SaleTime ?? DateTime.Now).CompareTo(PreCheckoutTime) > 0 && (s.IsExchange ?? false) == false).Select(s => (s.Good.SalePrice ?? 0) * (s.SaleNumber ?? 0)).Sum(s => s);
            var msg = new { Totalpage, TotalSale };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 销售挂账，同时返回数据,填充下面的挂账表
        /// </summary>
        /// <returns></returns>

        public ActionResult Sale_Credit(string id)
        {
            //在CreditSale中插入一个销售序列
            Model.CreditSale ad = new Model.CreditSale();
            ad.SaleUserID = OperateContext.Current.Usr.UserID;
            ad.CreditSaleBeginTime = DateTime.Now;
            ad.IsCheck = false;
            ad.CreditRoomID = Convert.ToInt32(id);
            OperateContext.Current.BLLSession.ICreditSaleBLL.Add(ad);
            //将CreditSetup的IsUesd设置为已经使用
            Model.CreditSetup m_CreditSetup_IsUse = new Model.CreditSetup() { CreditID = ad.CreditRoomID ?? 0, IsUsed = true };
            OperateContext.Current.BLLSession.ICreditSetupBLL.Modify(m_CreditSetup_IsUse, new string[] { "IsUsed" });

            //返回部分视图列表CreditSale
            var Sale_cr = OperateContext.Current.BLLSession.ICreditSaleBLL.GetListBy_NoTrack(s => s.IsCheck == false).Select(s => new Model.ViewModel.CreditSale()
            {
                CreditSaleNOID = s.CreditSaleNOID,
                CreditSetupName = s.CreditSetup.CreditName,
                CreditSetupID = s.CreditRoomID ?? 0,
                SaleUserName = s.UserDB.NickName,
                CreditSaleBeginTime = (s.CreditSaleBeginTime ?? DateTime.Now).ToString("yy年MM月dd日hh时mm分"),
                CheckMoney = s.CheckMoney ?? 0

            }).ToList();
            return PartialView("CreditSale", Sale_cr);
        }
    }
}
